package com.wuzhun.wxmp.config;

import com.wuzhun.wxmp.interceptor.AccessLimitInterceptor;
import com.wuzhun.wxmp.interceptor.LogInterceptor;
import com.wuzhun.wxmp.interceptor.AuthInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import static springfox.documentation.schema.AlternateTypeRules.newRule;

/**
 * @author baolin
 * @date 2022/12/03 14:24 下午
 */
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
//    @Resource
//    private TypeResolver typeResolver;

    @Value("${upload.file.path}")
    private String uploadPath;


    @Bean
    public AuthInterceptor getAuthInterceptor() {
        return new AuthInterceptor();
    }

    @Bean
    public LogInterceptor getLogInterceptor() {
        return new LogInterceptor();
    }

    @Bean
    public AccessLimitInterceptor getAccessLimitInterceptor() {
        return new AccessLimitInterceptor();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/statics/**")
                .addResourceLocations("classpath:/statics/");
        registry.addResourceHandler("/upload/**")
                .addResourceLocations("file:" + uploadPath);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getLogInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/error")
                .excludePathPatterns("/webjars/**")
                .excludePathPatterns("/statics/**");
        registry.addInterceptor(getAuthInterceptor())
                // addPathPatterns 用于添加拦截规则 ， 先把所有路径都加入拦截， 再一个个排除
                .addPathPatterns("/**")
                .excludePathPatterns("/error")
                .excludePathPatterns("/webjars/**")
                .excludePathPatterns("/statics/**")
                .excludePathPatterns("/admin/user/login");
        registry.addInterceptor(getAccessLimitInterceptor());
    }
}
